# coding=utf-8
# creator: Lu
# creat_time: 2021-11-9 13:48:56
import datetime
import os


def get_now_datetime():
    """
    %Y-%m-%d %H:%M:%S
    :return: 当前时间
    """
    t_format = "%Y-%m-%d %H:%M:%S"
    n = datetime.datetime.now()

    return n.strftime(t_format)


class WriteReport:
    def __init__(self, file_name, path=None, console=True):
        if not path:
            log_path = os.getcwd()
            path = log_path + "/../log/"

        self.home_path = path
        self.console = console

        self.file_name = file_name
        self.suffix = ".log"

    def write_info_log(self, con):
        """
        在指定目录生成 info 日志文件
        """
        if not con:
            return

        tm = get_now_datetime()
        with open(self.home_path + self.file_name + self.suffix, "a", encoding="utf-8") as f:
            con = "[I][{}] {}".format(tm, con)
            f.write(con)
            f.write("\n")

        if self.console:
            print(con)

    def write_error_log(self, con):
        """
        在指定目录生成 error 日志文件
        """
        if not con:
            return

        tm = get_now_datetime()
        with open(self.home_path + self.file_name + self.suffix, "a", encoding="utf-8") as f:
            con = "[E][{}] {}".format(tm, con)
            f.write(con)
            f.write("\n")

        if self.console:
            print(con)


if __name__ == '__main__':
    report = WriteReport("ds")

    report.write_info_log("sss")
    report.write_error_log("err")
